import { getToken } from "@/utils/auth";
import useUserStore from "@/store/modules/user";

const userInfo = useUserStore();

let webSock = null;
let datatime = 0;
let global_callback = null;
let isConnect = false; //连接标识 避免重复连接
let rec; //断线重连后，延迟5秒重新创建WebSocket连接  rec用来存储延迟请求的代码
let socketParams = {
  pageKey: -1,
};
let isActiveClose = false; // 是否主动关闭socket
// ws://192.168.3.22:7788/digital-human/websocket/2
let serverPort = "9500"; // webSocket连接端口
let wsUri =
  "ws://" + "192.168.3.22" + ":" + serverPort + "/websocket/" + userInfo.id;
// let serverPort = "8081"; // webSocket连接端口
// let wsUri = "ws://" + "192.168.50.156" + ":" + serverPort;

function createWebSocket(callback) {
  if (
    webSock == null ||
    Object.prototype.toString.call(webSock) !== "[object WebSocket]"
  ) {
    initWebSocket(callback);
  }
}

function initWebSocket(callback) {
  console.log("init webSocket");
  global_callback = callback;
  // 初始化websocket
  webSock = new WebSocket(`${wsUri}?Authorization=${getToken()}`);
  webSock.binaryType = "arraybuffer";
  webSock.buffer = null;
  webSock.audioUrl = "";
  webSock.imageQueue = '';
  webSock.onmessage = function (e) {
    websocketOnMessage(e);
  };
  webSock.onclose = function (e) {
    websocketClose(e);
  };
  webSock.onopen = function () {
    websocketOpen();
  };

  // 连接发生错误的回调方法
  webSock.onerror = function () {
    websocketError();
  };
}

//心跳设置
const heartCheck = {
  timeout: 200000, //每段时间发送一次心跳包 这里设置为20s
  timeoutObj: null, //延时发送消息对象（启动心跳新建这个对象，收到消息后重置对象）

  start: function () {
    this.timeoutObj = setInterval(function () {
      if (isConnect) webSock.send(JSON.stringify(socketParams));
    }, this.timeout);
  },

  reset: function () {
    clearTimeout(this.timeoutObj);
    this.start();
  },
};

//定义重连函数
let reConnect = () => {
  console.log("尝试重新连接");
  if (isConnect) return; //如果已经连上就不在重连了
  rec && clearTimeout(rec);
  rec = setTimeout(function () {
    // 延迟5秒重连  避免过多次过频繁请求重连
    if (!isConnect) {
      initWebSocket();
    }
  }, 5000);
};

// 实际调用的方法
function sendSock(agentData) {
  if (webSock.readyState === webSock.OPEN) {
    // 若是ws开启状态
    websocketSend(agentData);
  } else if (webSock.readyState === webSock.CONNECTING) {
    // 若是 正在开启状态，则等待1s后重新调用
    setTimeout(function () {
      sendSock(agentData);
    }, 1000);
  } else {
    // 若未开启 ，则等待1s后重新调用
    setTimeout(function () {
      sendSock(agentData);
    }, 1000);
  }
}

function closeSock({ activeClose = false }) {
  console.log(`关闭了 activeClose = ${activeClose}`);
  isActiveClose = activeClose;
  // 清除心跳定时器
  heartCheck.timeoutObj && clearTimeout(heartCheck.timeoutObj);
  // 清除重连定时器
  // console.log(isActiveClose);
  rec && clearTimeout(rec);
  if (isActiveClose) {
    // 关闭socket
    console.log(`关闭了`);
    webSock.close();
   
  }
  // 初始化相关变量
  webSock = null;
  isConnect = false;
}

// 数据接收
function websocketOnMessage(msg) {
  // global_callback(JSON.parse(msg.data));
  if (!msg || !msg.data) {
    // 可能得情况 - 心跳机制、无关信息接收
   
    return;
  }
  // console.log("收到数据：" + msg.data);

  if (
    typeof msg.data == "string" &&
    msg.data != "连接成功" &&
   JSON.parse( msg.data).url.includes("http")
  ) {
    // console.log(msg.data, "音乐");
    webSock.audioUrl = JSON.parse(msg.data).url;
    if (isJSON(msg.data)) {
      return global_callback(JSON.parse(msg.data).url,'audio',JSON.parse(msg.data).duration);
    } else {
      return global_callback(msg.data,'audio',msg.data.duration);
    }
  }
  if (msg.data != "连接成功" ) {
  
    const reader = new FileReader();
      // 将ArrayBuffer转换为Uint8Array
      const uint8Array = new Uint8Array(msg.data);

      webSock.buffer += String.fromCharCode.apply(null, uint8Array);
      console.log("图片消息时间为：", Date.now());
      // -this.datatime
    // Check for end message
    const endIndex = webSock.buffer.indexOf("END");
    if (endIndex !== -1) {
      const imageData = webSock.buffer.substring(0, endIndex);
      const imageBlob = new Blob(
        [new Uint8Array(imageData.split("").map((c) => c.charCodeAt(0)))],
        {
          type: "image/jpeg",
        }
      );

      // Create URL for the image blob
      const imageUrl = URL.createObjectURL(imageBlob);
     
      // Add the image URL to the queue
      // webSock.imageQueue.push(imageUrl);
      webSock.imageQueue=imageUrl;
      // Clear buffer
      webSock.buffer = webSock.buffer.substring(endIndex + 3);
    } 

    // console.log(webSock, "websock实例");

    if (isJSON(msg.data)) {
      return global_callback(JSON.parse(msg.data),'img');
    } else {
      return global_callback(msg.data,'img');
    }
  }
  /*
  // 收到信息为Blob类型时
  let result = null;
  if (msg.data instanceof Blob) {
    const reader = new FileReader();
    reader.readAsText(msg.data, "UTF-8");
    reader.onload = (e) => {
      // result = JSON.parse(reader.result);
      // console.log("Blobwebsocket收到", result);
    
    };
  } else {
    result = JSON.parse(msg.data);
    console.log("websocket收到", result);
    global_callback(result);
  }
    */
}

// 数据发送
function websocketSend(agentData) {
  // console.log("发送数据：" + agentData);
  webSock.send(agentData);
}

// 关闭
function websocketClose(e) {
  console.log("connection closed (" + e.code + ")");
  if (isActiveClose) {
    isActiveClose = false;
    return;
  }
  closeSock({ activeClose: false });
  // 执行重连
  reConnect();
}

const websocketError = () => {
  console.log("WebSocket连接发生错误");
  closeSock({ activeClose: true });
  // 执行重连
  reConnect();
};

function websocketOpen(e) {
  console.log("连接打开");
  isConnect = true;
  heartCheck.start(); //发送心跳 看个人项目需求
}
function isJSON(str) {
  if (typeof str == "string") {
    try {
      var obj = JSON.parse(str);

      if (typeof obj == "object" && obj) {
        return true;
      } else {
        return false;
      }
    } catch (e) {
      // console.log('error：'+str+'!!!'+e);
      return false;
    }
  }
  // console.log('It is not a string!')
}
export { sendSock, createWebSocket,websocketOnMessage, closeSock, webSock };
